package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加商品到购物车
     * @param cart
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){
        cartService.addCart(cart);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Cart>> queryCartList(){
        List<Cart> carts = this.cartService.queryCartList();
        if (carts == null){
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.ok(carts);
    }


    /**
     * 修改购物车中商品的数量
     * @param id
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> modifyNum(@RequestParam("id") String id,@RequestParam("num") Integer num){
        this.cartService.modifyNum(id,num);
        return ResponseEntity.ok().build();
    }


    /**
     * 批量添加购物车数据到redis中
     * @param carts
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<Void> addCartPatch(@RequestBody List<Cart> carts){

        this.cartService.addCartPatch(carts);
        return ResponseEntity.ok().build();
    }
}
